Node.js Message Reference

Node.js HTTP செய்தி பொருள்கள் மற்றும் அவற்றின் பண்புகள் மற்றும் முறைகள் பற்றி அறிக

HTTP செய்தி பொருள்

http.IncomingMessage பொருள் http.Server அல்லது http.ClientRequest மூலம் உருவாக்கப்பட்டு முறையே 'request' மற்றும் 'response' நிகழ்வுகளுக்கு முதல் வாதமாக அனுப்பப்படுகிறது. இது பதில் நிலை, தலைப்புகள் மற்றும் தரவை அணுக பயன்படுகிறது.

இந்த செய்தி பொருள் இரண்டையும் குறிக்கிறது:

இது Readable Stream இடைமுகத்தை செயல்படுத்துகிறது, இது செய்தி உடலை நுகர உங்களை அனுமதிக்கிறது.

சேவையக பக்க செய்தி

கோரிக்கை தகவல்கள், தலைப்புகள் மற்றும் உடல் தரவை அணுகுகிறது

கிளையன்ட் பக்க செய்தி

பதில் தகவல்கள், நிலை குறியீடுகள் மற்றும் உடல் தரவை அணுகுகிறது

ஸ்ட்ரீம் இடைமுகம்

Readable Stream இடைமுகத்தை செயல்படுத்துகிறது, தரவை திறம்பட கையாள உதவுகிறது

செய்தி பண்புகள்

பண்பு விளக்கம்
message.complete முழு செய்தியும் பெறப்பட்டு பாகுபடுத்தப்பட்டதா என்பதைக் குறிக்கும் பூலியன்
message.headers கோரிக்கை/பதில் தலைப்புகள் பொருள்
message.httpVersion கிளையன்ட் அனுப்பிய HTTP பதிப்பு. பொதுவாக '1.0' அல்லது '1.1'
message.method கோரிக்கை முறை ஒரு சரமாக (எ.கா., 'GET', 'POST'). http.Server இலிருந்து கோரிக்கை செய்திகளுக்கு மட்டுமே செல்லுபடியாகும்
message.rawHeaders அவை பெறப்பட்டதைப் போலவே மூல கோரிக்கை/பதில் தலைப்புகள் பட்டியல். ஒற்றைப்படை உள்ளீடுகள் விசை பெயர்கள், இரட்டைப்படை உள்ளீடுகள் மதிப்புகள்
message.rawTrailers அவை பெறப்பட்டதைப் போலவே மூல கோரிக்கை/பதில் டிரெய்லர் விசைகள் மற்றும் மதிப்புகள்
message.socket இணைப்புடன் தொடர்புடைய net.Socket பொருள்
message.statusCode HTTP பதில் நிலை குறியீடு. http.ClientRequest இலிருந்து பதில் செய்திகளுக்கு மட்டுமே செல்லுபடியாகும்
message.statusMessage HTTP பதில் நிலை செய்தி. http.ClientRequest இலிருந்து பதில் செய்திகளுக்கு மட்டுமே செல்லுபடியாகும்
message.trailers கோரிக்கை/பதில் டிரெய்லர் தலைப்புகள் பொருள்
message.url கோரிக்கை URL சரம். http.Server இலிருந்து கோரிக்கை செய்திகளுக்கு மட்டுமே செல்லுபடியாகும்

செய்தி முறைகள்

முறை விளக்கம்
message.destroy([error]) இந்த செய்தியைப் பெற்ற சாக்கெட்டில் destroy() ஐ அழைக்கிறது. error வழங்கப்பட்டால், ஒரு 'error' நிகழ்வு உமிழப்படும் மற்றும் பிழை ஒரு வாதமாக அனுப்பப்படும்
message.setTimeout(msecs, callback) socket.setTimeout(msecs, callback) ஐ அழைக்கிறது

Readable Stream ஆக செய்தி

http.IncomingMessage Readable Stream இடைமுகத்தை செயல்படுத்துகிறது, இது செய்தி உடலை நுகர உங்களை அனுமதிக்கிறது.

http.IncomingMessage Readable Stream இடைமுகத்தை செயல்படுத்துவதால், இது read(), pipe() போன்ற அனைத்து ஸ்ட்ரீம் முறைகளையும் 'data', 'end', மற்றும் 'error' போன்ற நிகழ்வுகளையும் உள்ளடக்கியது.

💡 ஸ்ட்ரீம் நன்மைகள்:

  • நினைவக திறன்: பெரிய தரவை துண்டுகளாக செயலாக்குகிறது
  • பின்னழுத்தம் கையாளுதல்: தரவு ஓட்டத்தை தானாகவே கட்டுப்படுத்துகிறது
  • திறன்: தரவு கிடைக்கும் விரைவில் செயலாக்கத்தை தொடங்குகிறது

எடுத்துக்காட்டுகள்

சேவையக பக்க செய்தி (கோரிக்கை)

இந்த எடுத்துக்காட்டு சேவையக பக்கத்தில் IncomingMessage பொருளைக் கையாளுவதை நிரூபிக்கிறது:

const http = require('http');
const url = require('url');

// Create an HTTP server
const server = http.createServer((req, res) => {
  // 'req' is the IncomingMessage object
  
  // Basic message properties
  console.log('HTTP Version:', req.httpVersion);
  console.log('Method:', req.method);
  console.log('URL:', req.url);
  
  // Parse the URL
  const parsedUrl = url.parse(req.url, true);
  console.log('Pathname:', parsedUrl.pathname);
  console.log('Query:', parsedUrl.query);
  
  // Headers
  console.log('Headers:', req.headers);
  console.log('User-Agent:', req.headers['user-agent']);
  
  // Raw headers with keys and values as separate array elements
  console.log('Raw Headers:', req.rawHeaders);
  
  // Socket information
  console.log('Remote Address:', req.socket.remoteAddress);
  console.log('Remote Port:', req.socket.remotePort);
  
  // Reading the message body (if any)
  let body = [];
  req.on('data', (chunk) => {
    body.push(chunk);
  });
  
  req.on('end', () => {
    body = Buffer.concat(body).toString();
    console.log('Request body:', body);
    
    // Now that we have the body, send a response
    res.writeHead(200, { 'Content-Type': 'application/json' });
    res.end(JSON.stringify({
      httpVersion: req.httpVersion,
      method: req.method,
      url: req.url,
      headers: req.headers,
      body: body || null
    }));
  });
  
  // Handle errors
  req.on('error', (err) => {
    console.error('Request error:', err);
    res.statusCode = 400;
    res.end('Error: ' + err.message);
  });
});

// Start server
const PORT = 8080;
server.listen(PORT, () => {
  console.log(`Server running at http://localhost:${PORT}/`);
  
  // Make a test request
  http.request({
    hostname: 'localhost',
    port: PORT,
    path: '/test?param1=value1¶m2=value2',
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Custom-Header': 'Custom Value'
    }
  }, (res) => {
    res.resume(); // Consume response data
  }).end('{"message":"Hello from the client!"}');
});

கிளையன்ட் பக்க செய்தி (பதில்)

இந்த எடுத்துக்காட்டு கிளையன்ட் பக்கத்தில் IncomingMessage பொருளைக் கையாளுவதை நிரூபிக்கிறது:

const http = require('http');

// Make an HTTP request
const req = http.request('http://example.com', (res) => {
  // 'res' is the IncomingMessage object (response)
  
  // Basic message properties
  console.log('Status Code:', res.statusCode);
  console.log('Status Message:', res.statusMessage);
  console.log('HTTP Version:', res.httpVersion);
  
  // Headers
  console.log('Headers:', res.headers);
  console.log('Content-Type:', res.headers['content-type']);
  console.log('Raw Headers:', res.rawHeaders);
  
  // Socket information
  console.log('Remote Address:', res.socket.remoteAddress);
  console.log('Remote Port:', res.socket.remotePort);
  
  // Reading the message body
  let body = [];
  
  // Data events emit when chunks of the body are received
  res.on('data', (chunk) => {
    body.push(chunk);
    console.log('Received chunk of', chunk.length, 'bytes');
  });
  
  // End event is emitted when the entire body has been received
  res.on('end', () => {
    body = Buffer.concat(body).toString();
    console.log('Body length:', body.length);
    console.log('Body preview:', body.substring(0, 100) + '...');
    
    // Check trailers (if any)
    console.log('Trailers:', res.trailers);
    console.log('Raw Trailers:', res.rawTrailers);
    
    // Check if message is complete
    console.log('Message complete:', res.complete);
  });
  
  // Handle message errors
  res.on('error', (err) => {
    console.error('Response error:', err);
  });
});

// Handle request errors
req.on('error', (err) => {
  console.error('Request error:', err);
});

// End the request
req.end();

ஸ்ட்ரீம்களுடன் செய்தி உடலைக் கையாளுதல்

இந்த எடுத்துக்காட்டு கோப்பு பதிவேற்றங்களை கையாள ஒரு சேவையகத்தை உருவாக்கி, செய்தி உடலை நேரடியாக ஒரு கோப்புக்கு ஸ்ட்ரீம் செய்கிறது:

const http = require('http');
const fs = require('fs');
const path = require('path');

// Create a server to handle file uploads
const server = http.createServer((req, res) => {
  if (req.method === 'POST' && req.url === '/upload') {
    // Create a write stream to a file
    const filePath = path.join(__dirname, 'uploaded-file.txt');
    const fileStream = fs.createWriteStream(filePath);
    
    // Pipe the request body directly to the file
    req.pipe(fileStream);
    
    // Handle completion
    fileStream.on('finish', () => {
      // Get file stats to check size
      fs.stat(filePath, (err, stats) => {
        if (err) {
          console.error('Error getting file stats:', err);
          res.writeHead(500, {'Content-Type': 'text/plain'});
          res.end('Error processing upload');
          return;
        }
        
        // Send response
        res.writeHead(200, {'Content-Type': 'application/json'});
        res.end(JSON.stringify({
          success: true,
          message: 'File uploaded successfully',
          size: stats.size,
          path: filePath
        }));
        
        console.log(`File uploaded to ${filePath}`);
        console.log(`File size: ${stats.size} bytes`);
        
        // Clean up the file after a delay
        setTimeout(() => {
          fs.unlink(filePath, (err) => {
            if (err) console.error('Error removing uploaded file:', err);
            else console.log('Uploaded file removed');
          });
        }, 5000);
      });
    });
    
    // Handle errors
    fileStream.on('error', (err) => {
      console.error('File write error:', err);
      res.writeHead(500, {'Content-Type': 'text/plain'});
      res.end('Error saving file');
    });
    
    req.on('error', (err) => {
      console.error('Request error:', err);
      fileStream.destroy(err);
    });
  }
  else if (req.method === 'GET' && req.url === '/') {
    // Provide a simple HTML form for uploading
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.end(`
      
      
      
        File Upload Example
      
      
        

Upload a Text File

Note: This is a simple example. A real implementation would need to parse multipart form data.

`); } else { // Handle all other requests res.writeHead(404, {'Content-Type': 'text/plain'}); res.end('Not Found'); } }); // Start server const PORT = 8080; server.listen(PORT, () => { console.log(`Server running at http://localhost:${PORT}/`); // Make a test upload setTimeout(() => { const req = http.request({ hostname: 'localhost', port: PORT, path: '/upload', method: 'POST', headers: { 'Content-Type': 'text/plain' } }, (res) => { let data = ''; res.on('data', (chunk) => { data += chunk; }); res.on('end', () => { console.log('Upload response:', data); }); }); req.on('error', (e) => { console.error('Test request error:', e.message); }); // Write some content to upload req.write('This is a test file content uploaded using http.request.\n'); req.write('It demonstrates streaming data to the server.\n'); req.end(); }, 1000); });

செய்தி டிரெய்லர்களைக் கையாளுதல்

இந்த எடுத்துக்காட்டு HTTP டிரெய்லர்களை (செய்தி உடலுக்குப் பிறகு வரும் தலைப்புகள்) கையாளுவதை நிரூபிக்கிறது:

const http = require('http');
const zlib = require('zlib');

// Create an HTTP server that sends trailers
const server = http.createServer((req, res) => {
  // Inform the client we'll be sending trailers
  res.writeHead(200, {
    'Content-Type': 'text/plain',
    'Transfer-Encoding': 'chunked', // Required for trailers
    'Trailer': 'Content-MD5, X-Response-Time' // Declare which trailers will be sent
  });
  
  // Write some response data
  res.write('Beginning of the response\n');
  
  // Simulate processing time
  setTimeout(() => {
    res.write('Middle of the response\n');
    
    setTimeout(() => {
      // Final part of the body
      res.write('End of the response\n');
      
      // Add trailers
      res.addTrailers({
        'Content-MD5': 'e4e68fb7bd0e697a0ae8f1bb342846d3', // Would normally be the hash of the body
        'X-Response-Time': `${Date.now() - req.start}ms` // Processing time
      });
      
      // End the response
      res.end();
    }, 500);
  }, 500);
});

// Track request start time
server.on('request', (req) => {
  req.start = Date.now();
});

// Start server
const PORT = 8080;
server.listen(PORT, () => {
  console.log(`Server running at http://localhost:${PORT}/`);
  
  // Make a request to test trailers
  http.get(`http://localhost:${PORT}`, (res) => {
    console.log('Response status:', res.statusCode);
    console.log('Response headers:', res.headers);
    
    // Check if trailers are declared
    if (res.headers.trailer) {
      console.log('Trailer headers declared:', res.headers.trailer);
    }
    
    // Read the response body
    let body = '';
    
    res.on('data', (chunk) => {
      body += chunk;
      console.log('Received chunk:', chunk.toString());
    });
    
    // The 'end' event is emitted when the entire body has been received
    res.on('end', () => {
      console.log('Complete response body:', body);
      console.log('Trailers received:', res.trailers);
      
      // Server should close after test is complete
      server.close();
    });
  }).on('error', (err) => {
    console.error('Request error:', err);
  });
});

ஓட்டக் கட்டுப்பாட்டுடன் பெரிய செய்திகளைக் கையாளுதல்

இந்த எடுத்துக்காட்டு ஓட்டக் கட்டுப்பாட்டுடன் பெரிய செய்தி உடல்களைக் கையாளுவதை நிரூபிக்கிறது:

const http = require('http');

// Create a server to handle large uploads with flow control
const server = http.createServer((req, res) => {
  if (req.method === 'POST' && req.url === '/large-upload') {
    // Set up variables to track data
    let dataSize = 0;
    let chunks = 0;
    
    // Switch to pause mode (by default it's in flowing mode)
    req.pause();
    
    console.log('Incoming large upload - using flow control');
    
    // Process data in chunks
    function processNextChunk() {
      // Resume the stream to get more data
      req.resume();
      
      // Set a timeout to pause after a bit
      setTimeout(() => {
        // Pause the stream again
        req.pause();
        
        console.log(`Processed chunk ${++chunks}, total ${dataSize} bytes so far`);
        
        // If there's more data to process, schedule the next chunk
        // Otherwise, wait for 'end' event to finish
        if (!req.complete) {
          // Schedule next chunk processing
          setTimeout(processNextChunk, 100);
        }
      }, 100); // Process for 100ms, then pause
    }
    
    // Listen for data events
    req.on('data', (chunk) => {
      dataSize += chunk.length;
    });
    
    // Handle request end
    req.on('end', () => {
      console.log(`Upload complete: ${dataSize} bytes received in ${chunks} chunks`);
      
      // Send a response
      res.writeHead(200, {'Content-Type': 'application/json'});
      res.end(JSON.stringify({
        success: true,
        bytesReceived: dataSize,
        chunks: chunks
      }));
    });
    
    // Handle errors
    req.on('error', (err) => {
      console.error('Request error:', err);
      
      res.writeHead(500, {'Content-Type': 'text/plain'});
      res.end('Error processing upload: ' + err.message);
    });
    
    // Start processing
    processNextChunk();
  }
  else {
    // Handle other requests
    res.writeHead(404, {'Content-Type': 'text/plain'});
    res.end('Not Found');
  }
});

// Start server
const PORT = 8080;
server.listen(PORT, () => {
  console.log(`Server running at http://localhost:${PORT}/`);
  
  // Create a test client to simulate large upload
  console.log('Simulating large upload...');
  
  const req = http.request({
    hostname: 'localhost',
    port: PORT,
    path: '/large-upload',
    method: 'POST',
    headers: {
      'Content-Type': 'application/octet-stream'
    }
  }, (res) => {
    // Handle response
    let responseData = '';
    
    res.on('data', (chunk) => {
      responseData += chunk;
    });
    
    res.on('end', () => {
      console.log('Server response:', responseData);
      
      // Close the server after the test
      server.close();
    });
  });
  
  req.on('error', (err) => {
    console.error('Upload request error:', err);
  });
  
  // Generate and send a large body in chunks
  function sendChunk(i, total) {
    if (i >= total) {
      // All chunks sent, end the request
      req.end();
      return;
    }
    
    // Create a 10KB chunk
    const chunk = Buffer.alloc(10240);
    chunk.fill(65 + (i % 26)); // Fill with repeating letters
    
    // Write the chunk
    const canContinue = req.write(chunk);
    
    // Log progress
    if (i % 10 === 0) {
      console.log(`Sent chunk ${i}/${total} (${i * 10240} bytes)`);
    }
    
    // If we can continue writing, schedule next chunk
    if (canContinue) {
      // Schedule next chunk
      setImmediate(() => sendChunk(i + 1, total));
    } else {
      // If backpressure is applied, wait for drain event
      console.log('Backpressure applied, waiting for drain');
      req.once('drain', () => {
        console.log('Drained, continuing upload');
        sendChunk(i + 1, total);
      });
    }
  }
  
  // Start sending chunks (50 chunks = ~500KB)
  sendChunk(0, 50);
});

சிறந்த நடைமுறைகள்

முழுமையான செய்திகளைச் சரிபார்க்கவும்: முழு செய்தியும் பெறப்பட்டுள்ளதா என்பதை உறுதிப்படுத்த message.complete ஐப் பயன்படுத்தவும்
பிழைகளைக் கையாளவும்: செய்தி பொருள்களில் 'error' நிகழ்வை எப்போதும் கேளுங்கள்
ஓட்டக் கட்டுப்பாடு: பெரிய செய்திகளுக்கு, தரவு ஓட்டத்தைக் கட்டுப்படுத்த pause() மற்றும் resume() ஐப் பயன்படுத்தவும்
ஸ்ட்ரீம் செயலாக்கம்: செய்தி உடல்களின் திறனான செயலாக்கத்திற்கு pipe() போன்ற ஸ்ட்ரீம் முறைகளைப் பயன்படுத்தவும்
நினைவக மேலாண்மை: பெரிய செய்திகளுக்கு, முழு செய்தியையும் நினைவகத்தில் ஏற்றுவதற்குப் பதிலாக துண்டுகளில் தரவைச் செயலாக்கவும்
URL பாகுபடுத்துதல்: request.url இலிருந்து URL சரங்களைப் பாகுபடுத்த url தொகுதியைப் பயன்படுத்தவும்
தலைப்பு கையாளுதல்: HTTP தலைப்புகள் case-insensitive என்பதை அறிந்து கொள்ளுங்கள், ஆனால் Node.js அவற்றை சிறிய எழுத்துக்களாக மாற்றுகிறது

பயிற்சி

Node.js இல் HTTP கோரிக்கை மற்றும் பதில் செய்திகளைக் குறிக்கும் பொருள் எது? தேர்வு செய்யவும்.

http.ServerMessage
✗ தவறு! "http.ServerMessage" என்பது Node.js இல் ஒரு செல்லுபடியாகும் பொருள் அல்ல
http.RequestObject
✗ தவறு! "http.RequestObject" என்பது Node.js இல் ஒரு செல்லுபடியாகும் பொருள் அல்ல
http.IncomingMessage
✓ சரி! "http.IncomingMessage" என்பது Node.js இல் HTTP கோரிக்கை மற்றும் பதில் செய்திகளைக் குறிக்கும் சரியான பொருளாகும்
http.MessageHandler
✗ தவறு! "http.MessageHandler" என்பது Node.js இல் ஒரு செல்லுபடியாகும் பொருள் அல்ல